package pl.softech.oldchart;

import java.awt.Color;
import java.awt.geom.Point2D;
import java.util.Collection;
import java.util.Map;

public abstract class AbstractGraph2DModel implements IGraph2DModel {
	
	private static int instancesQuantity = 0;
	private static final  Color DEFAULT_GRAPH_COLOR = Color.RED;
	
	private Point2D _maxPointCoordinate;
	private Point2D _minPointCoordinate;
	private String name;
	private Color graphColor = DEFAULT_GRAPH_COLOR;
	private boolean drawCoordinates = true;
	private boolean showXAxisLabelsFlag = false;
	private boolean showYAxisLabelsFlag = false;
	private boolean isSelectedFlag = false;
	
	public AbstractGraph2DModel() {
		++instancesQuantity;
		name = "graph" + instancesQuantity;
	}
	
	protected void recompute() {
		computeMinMaxCoord();
	}
	
	private void computeMinMaxCoord() {
		
		Collection<Point2D> points = getPoints();
		
		double maxX = Double.MIN_VALUE;
		double maxY = Double.MIN_VALUE;
		
		double minX = Double.MAX_VALUE;
		double minY = Double.MAX_VALUE;
		
		for(Point2D p : points) {
			maxX = Math.max(maxX, p.getX());
			maxY = Math.max(maxY, p.getY());
			
			minX = Math.min(minX, p.getX());
			minY = Math.min(minY, p.getY());
		}
		
		_maxPointCoordinate = new Point2D.Double(maxX, maxY);
		_minPointCoordinate = new Point2D.Double(minX, minY);

	}
	
	@Override
	public Color getGraphColor() { return graphColor; }

	@Override
	public void setGraphColor(Color color) { graphColor = color; }
	
	@Override
	public double getMaxX() {
		if(_maxPointCoordinate == null)
			computeMinMaxCoord();
		return _maxPointCoordinate.getX();
	}

	@Override
	public double getMaxY() {
		if(_maxPointCoordinate == null)
			computeMinMaxCoord();
		return _maxPointCoordinate.getY();
	}

	@Override
	public double getMinX() {
		if(_minPointCoordinate == null)
			computeMinMaxCoord();
		return _minPointCoordinate.getX();
	}

	@Override
	public double getMinY() {
		if(_minPointCoordinate == null)
			computeMinMaxCoord();
		return _minPointCoordinate.getY();
	}

	@Override
	public Map<Point2D, String> getPointsLabels() { return null; }

	@Override
	public Map<Double, String> getXAxisLabels() { return null; }

	@Override
	public Map<Double, String> getYAxisLabels() { return null; }

	@Override
	public boolean showXAxisLabels() { return showXAxisLabelsFlag; }
	
	@Override
	public void setShowXAxisLabels(boolean bFlag) { showXAxisLabelsFlag = bFlag; }

	@Override
	public boolean showYAxisLabels() { return showYAxisLabelsFlag; }
	
	@Override
	public void setShowYAxisLabels(boolean bFlag) { showYAxisLabelsFlag = bFlag; }

	@Override
	public String getName() { return name; }
	
	@Override
	public boolean drawCoordinates() { return drawCoordinates; }
	
	@Override
	public void setDrawCoordinates(boolean flag) { drawCoordinates = flag; }
	
	@Override
	public boolean isSelected() { return isSelectedFlag; }
	
	@Override
	public void setSelected(boolean bFlag) { isSelectedFlag = bFlag; }
}
